﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using WindowsInstaller;


//http://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET

//Automation Interface Reference
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa367810(v=vs.85).aspx

//C# Language Specification
//http://msdn.microsoft.com/en-us/library/aa645596(v=vs.71)

//SQL sintax
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa372021%28v=vs.85%29.aspx


namespace MsiValidator
{
    class dbConnection
    {
        private Type installerType;
        private Installer installer;
        private Database database;

        public dbConnection()
        {
            // Get the type of the Windows Installer object
            installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

            // Create the Windows Installer object
            installer = (Installer)Activator.CreateInstance(installerType);
        }

        public Database openDatabaseConnection(string msiDabataseFile, MsiOpenDatabaseMode msiOpenDbMode)
        {
            // Open the MSI database file
            database = installer.OpenDatabase(msiDabataseFile, msiOpenDbMode);
            return database;
        }

        public Record executeSelectQuery(string tableName, string query)
        {
            // Check if table exists
            if (database.get_TablePersistent(tableName) == WindowsInstaller.MsiEvaluateCondition.msiEvaluateConditionTrue)
            {
                // Open a view
                View view = database.OpenView(query);

                // Execute the view query
                view.Execute(null);

                // Get the record from the view
                Record record = view.Fetch();

                // close the database
                view.Close();

                return record;
            }
            else
            {
                //return table persistent state
                return null;
            }
        }

        public void executeInsertQuery(string tableName, string query)
        {
            View view = database.OpenView(query);
            view.Execute(null);
            // close the database
            view.Close();
            // save 
            database.Commit();
        }

        public void executeUpdateQuery(string query)
        {
            // Open a view on the Property table for the version property
            View view = database.OpenView(query);

            // Execute the view query
            view.Execute(null);

            // close the database
            view.Close();

            // save 
            database.Commit();
        }

        public void executeDeleteQuery(string query)
        {
            // Open a view on the Property table for the version property
            View view = database.OpenView("SELECT * FROM LaunchCondition WHERE Condition = 'MsiNTProductType=1'");

            // Execute the view query
            view.Execute(null);

            // close the database
            view.Close();

            // save 
            database.Commit();
        }
    }
}
